package com.yidu.ladder.delivery.controller;


import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yidu.ladder.base.domain.Staff;
import com.yidu.ladder.delivery.domain.BasicJobList;
import com.yidu.ladder.delivery.domain.BasicJobListVo;
import com.yidu.ladder.delivery.domain.JobList;
import com.yidu.ladder.delivery.domain.WorkSheetImport;
import com.yidu.ladder.delivery.service.WorkSheetImportService;
import com.yidu.ladder.util.AddressUtil;

/**
 * 工作单导入控制器
 * @author bamboo
 *
 */
@Controller
@RequestMapping("/workSheet/")
public class WorkSheetImportController {
	
	@Autowired
	private WorkSheetImportService importService;

	/**
	 * 得到工作单导入service
	 * @return 工作单导入service
	 */
	public WorkSheetImportService getImportService() {
		return importService;
	}
	/**
	 * 设置辅助档案service
	 * @param importService 工作单导入service
	 */
	public void setImportService(WorkSheetImportService importService) {
		this.importService = importService;
	}
	
	/**
	 * 工作单导入方法
	 * @param request 请求
	 * @return Excel文件数据
	 * @throws Exception 异常
	 */
	@RequestMapping("imports")
    @ResponseBody 
    public Map<String, Object> impotrs(HttpServletRequest request) throws Exception {
		//创建map对象
	   Map<String, Object> map = new HashMap<String, Object>();
	   //文件解析请求
       MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
       //得到文件对象
       MultipartFile file = multipart.getFile("file");
       //文件数据以输入流形式存放
       InputStream in = file.getInputStream();
        //调用service层数据导入方法得到解析数据
       List<WorkSheetImport> importUpload = importService.importUpload(in, file);
       //map对象存放service层解析到的数据
       map.put("data", importUpload);
       //关闭输入流
       in.close();
       //返回数据
       return map;
    }
	
	/**
	 * 查询数据
	 * @param basicJobListVo 基础工作单包装类
	 * @return 数据键值对
	 */
	@RequestMapping("joblists")
	@ResponseBody
	public Map<String, Object> findAll(BasicJobListVo basicJobListVo){
		//懒加载查询工作单集合方法
		return importService.lazySelectjobList(basicJobListVo);
	}
	
	/**
	 * 设置工作单号
	 * @param num 数量
	 * @return 基础工作单集合
	 */
	@PostMapping("generateNo/{nums}")
	@ResponseBody
	public List<BasicJobList> joblistNo(@PathVariable("nums") String num){
		//设置基础工作单
		return importService.setJobListNo(num);
		
	}
	
	/**
	 * 安排取派员
	 * @param address 拿到地址解析
	 * @return 职员集合
	 */
	@RequestMapping("arrangeStaff")
	@ResponseBody
	public List<Staff> arrangeStaff(String address){
		//调用地址工具类解析地址得到省市县
		Map<String, Object> map = AddressUtil.separateAddress(address);
		//解析地址得到省的字符串
		String provinces = map.get("province").toString();
		//解析地址得到市的字符串
		String citys = map.get("city").toString();
		//解析地址得到县的数据
		String districts = map.get("district").toString();
		//调用地址解析工具类将得到的省市县首字母大写
		String addressCode = AddressUtil.upperAddress(provinces, citys, districts);
		//调用service层根据地址编码查询职员方法
		List<Staff> list = importService.selectStaffByCode(addressCode);
		//返回职员集合
		return list;
	}
	
	/**
	 * 保存工作单数据
	 * @param jobLists 工作单集合
	 * @return 保存成功返回true，否则返回false
	 */
	@PostMapping("saveJoblist")
	@ResponseBody
	public boolean saveJoblist(@RequestBody List<JobList> jobLists) {
		//调用保存的方法并返回
		return importService.saveJoblist(jobLists);
	}
}
